#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#if defined(TEST_TARGET_romt)
#include <varch/command.h>
#include <varch/unitt.h>
#include <varch/romt.h>
#else  
#include "init.h"
#include "command.h"
#include "unitt.h"
#include "kern.h"
#include "romt.h"
#include "kern.h"
#endif

/************************************************************************************/
/************************************* Unit Test ************************************/
/************************************************************************************/

// #define EXIT_TEST
extern uint64_t unitt_clock(void);

static int test_0(void)
{
    for (int i = 0; i < 100; i++)
    {
        if (0) 
        {
            
            #if defined (EXIT_TEST)
            exit(0);
            #endif 
            return UNITT_E_FAIL;
        }
    }
    
    return UNITT_E_OK;
}

static void unitt_task(void)
{
    static UNITT_TCASE rand_tests[] = {
        UNITT_TCASE(test_0),
        // UNITT_TCASE(test_1),
        // UNITT_TCASE(test_2),
    };

    static UNITT suites[] = {
        { "xxx suite", rand_tests, sizeof(rand_tests) / sizeof(rand_tests[0]) , unitt_clock },
    };

    UNITT_EXE(suites);
}

/************************************************************************************/
/************************************* Base Test ************************************/
/************************************************************************************/

#define TASK_PERIOD         5 // ms

static uint8_t buffer[1024];
static ROMT romt_object;

static int rom_read(uint32_t address, uint8_t *data, uint32_t length)
{
    uint32_t i = 0;

    if (address + length > sizeof(buffer))
    {
        return 0;
    }

    for (i = 0; i < length; i++)
    {
        data[i] = buffer[address + i];
    }

    return i;

}

static int romt_write(uint32_t address, uint8_t *data, uint32_t length)
{
    uint32_t i = 0;

    if (address + length > sizeof(buffer))
    {
        return 0;
    }

    for (i = 0; i < length; i++)
    {
        buffer[address + i] = data[i];
    }

    return i;
}

static void test_task(void)
{
    static int count = 0;

    romt_task(&romt_object);

    if (count % 1000 == 0)
    {
        printf("result %08X\r\n", romt_result(&romt_object));
    }

    count += TASK_PERIOD; 
}

static void test_base(void)
{
    romt_object.base = 0;
    romt_object.size = sizeof(buffer);
    romt_object.read = rom_read;
    romt_object.write = romt_write;
    romt_init(&romt_object);

    romt_start(&romt_object, ROMT_MODE_NORMAL, 0xFFFFFFFF);

#if defined(TEST_TARGET_romt)
    while (1)
    {
        test_task();
        usleep(1000 * period);
    }
#else  
    printf("create task %d\r\n", task_create(TASK_PERIOD, test_task));
#endif
}

/************************************************************************************/
/*************************************  Command  ************************************/
/************************************************************************************/

static void usage(void)
{
    printf(
"Usage: romt [opt] [arg] ...\n"
"\n"
"options:\n"
"    -e <execute>        Specifies the function to execute, the default is the <base> test\n"
"                        <base>      Test base function\n"
"                        <ut>        Unit test\n"
"    -h                  Print help\n"
"    -v                  Print version\n"
"    -u [<period>]       Unit test period, unit ms, the default is 1000ms\n"
"\n"

    );
}

static int test(int argc, char *argv[])
{
    char *execute = NULL;
    int ut_period = 1000;
    int period = 1000; // ms

    /* reset getopt */
    command_opt_init();

    while (1)
    {
        int opt = command_getopt(argc, argv, "e:hvu::p:");
        if (opt == -1) break;

        switch (opt) 
        {
        case 'p' :
            period = atoi(command_optarg);
            break;
        case 'u' :
            if (command_optarg) ut_period = atoi(command_optarg);
            break;
        case 'e' :
            execute = command_optarg;
            break;
        case 'v' :
            printf("romt version %d.%d.%d\r\n", ROMT_V_MAJOR, ROMT_V_MINOR, ROMT_V_PATCH);
            return 0;
        case '?':
            printf("Unknown option `%c`\r\n", command_optopt);
            return -1;
        case 'h' : 
        default:
            usage();
            return 0;
        }
    }

    if (execute)
    {
        if (!strcmp(execute, "base"))
        {
            test_base();
        }
        else if (!strcmp(execute, "ut"))
        {
            #if defined(TEST_TARGET_romt)
            while (1)
            {
                unitt_task();
                usleep(1000 * ut_period);
            }
            #else  
            printf("create task %d\r\n", task_create(ut_period, unitt_task));
            #endif
        }
    }
    else  
    {
        test_base();
    }
    
    return 0;
}

/************************************************************************************/
/************************************ Test entry ************************************/
/************************************************************************************/

#if defined(TEST_TARGET_romt)
int main(int argc, char *argv[])
{
    return test(argc, argv);
}
#else 
void test_romt(void)
{
    command_export("romt", test);
}
init_export_app(test_romt);
#endif 
